import java.io.*;
import java.util.*;

public class Parser {
    private HashMap<String, TextBounds> textMetaData = new HashMap<String, TextBounds>();

    public Parser() {
        scanFiles();
    }

    // Find text by a key
    public byte[] get (String key) {
        TextBounds tb = textMetaData.get(key);
        byte[] result = null;
        if (tb != null) {
            try {
                RandomAccessFile raf = new RandomAccessFile(tb.getFileName(), "r");
                int length = textLength(raf, tb);
                result = new byte[length];
                raf.seek(tb.getOffset());
                int i = raf.read(result, 0, length);
            }
            catch (IOException e) {
                System.out.println("Can't open file");
            }
        }
        return result;
    }

    // Determine length of text associated with a key
    private int textLength(RandomAccessFile raf, TextBounds bounds) throws IOException {
        int length = 0;
        String line;

        raf.seek(bounds.getOffset() - 1);
        while (true) {
            line = raf.readLine();
            if (line == null) {
                length = ((int) raf.length()) - bounds.getOffset();
                break;
            }
            if (line.startsWith("===") && line.endsWith("===")) {
                length = ((int) raf.getFilePointer()) - line.length() - bounds.getOffset() - 3;
                break;
            }
        }
        return length;
    }

    // Get a list of files in current directory
    private String[] fileList() {
        String[] list = new File(".").list();
        return list;
    }

    // Searching for keys in files
    private void scanFiles() {
        String[] fileList = fileList();

        for (String name : fileList) {
            if (name.endsWith(".txt"))
                parseFile(name);
        }
    }

    // Parse file to create meta data about keys
    private void parseFile(String fileName) {
        RandomAccessFile reader = null;

        try {
            reader = new RandomAccessFile(fileName, "r");
            createTextMetaData(reader, fileName);
        }
        catch (IOException e) {
            System.out.println("Can't parse file");
        }
        finally {
            closeStream(reader);
        }
    }

    // Create a key meta data structure
    private void createTextMetaData(RandomAccessFile reader, String fileName) throws IOException {
        String line;
        String label = "";
        int offset = 0;

        while (true) {
            line = reader.readLine();
            if (line == null) break;
            if (line.startsWith("===") && line.endsWith("===")) {
                TextBounds bounds = new TextBounds();
                label = line.substring(3, line.length() - 3);
                offset = (int) reader.getFilePointer();
                bounds.setFileName(fileName);
                bounds.setOffset(offset);
                textMetaData.put(label, bounds);
            }
        }
    }

    // Close random access file stream
    private void closeStream(RandomAccessFile r) {
        try {
            r.close();
        }
        catch (IOException e) {
            System.out.println("Can't close stream");
        }
    }
}


